package cn.school.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.school.entity.Student;
import cn.school.service.Service;
import cn.school.util.StringKit;

/**
 * Servlet implementation class StudentLimitServlet
 */
@WebServlet("/StudentLimitServlet")
public class StudentLimitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StudentLimitServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/html;charset=utf-8");
		String sname = StringKit.converNull(request.getParameter("sname"));
		String telephone = StringKit.converNull(request.getParameter("telephone"));
		String qq = StringKit.converNull(request.getParameter("qq"));
		String enterdatestart = StringKit.converNull(request.getParameter("enterdatestart"));
		Date enterdatestart1 = StringKit.toDate(request.getParameter("enterdatestart"));
		/* SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd"); */

		Service service = new Service();
		int pageno = 1;
		try {
			pageno = Integer.parseInt(request.getParameter("pageno"));
		} catch (Exception e) {
		}
		request.setAttribute("sname", sname);
		request.setAttribute("telephone", telephone);
		request.setAttribute("qq", qq);
		request.setAttribute("enterdatestart", enterdatestart);

		List<Student> list = service.searchLimitByMohu(pageno, 10, sname, telephone, qq, enterdatestart1);
		int total = service.searchTotal(sname, telephone, qq, enterdatestart1);
		PrintWriter out = response.getWriter();
		out.print(total);
		String ss = JSON.toJSONString(list);
		out.print(ss);

		int pageTotal = total / 10 + (total % 10 == 0 ? 0 : 1);
		request.setAttribute("pageno", pageno);
		request.setAttribute("pageTotal", pageTotal);
		request.setAttribute("list", list);

		request.getRequestDispatcher("newStudent.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
